package com.xingda.packing.dmds.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmds.domain.PackProductionLine;
import com.xingda.packing.dmds.enums.TaskLocation;
import com.xingda.packing.dmds.enums.WheelHeight;
import com.xingda.packing.dmds.enums.WorkAreaType;
import com.xingda.packing.dmds.minitor.entity.PkBoxLocation;
import com.xingda.packing.dmds.minitor.mapper.PkBoxLocationMapper;
import com.xingda.packing.dmds.minitor.service.MachineStatusService;
import com.xingda.packing.dmds.plc.service.PlcDirectiveService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("machineStatusDMDSService")
public class MachineStatusServiceImpl implements MachineStatusService {
    @Resource
    private PlcDirectiveService plcDirectiveService;
    @Resource
    private PkBoxLocationMapper pkBoxLocationMapper;
    @Resource
    private PackProductionLine packProductionLine;
    @Value("${com.xingda.version}")
    @Getter
    private String version;

    @Override
    public List<String> status(int area) {
        List<String> list = new ArrayList<>();
        if (area < 3) {
            list.add("机器人-" + area + "MES 版本号:" + version);
            list.add(WorkAreaType.getByID(area).getTypeName() + "-可以下发任务:" + (plcDirectiveService.canAssignTask(area) ? "可以" : "不可以"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-缓存位-装箱位是否工作:" + (packProductionLine.getRobot().getWorkStatus(area) ? "工作" : "停止"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-抓取完成:" + (plcDirectiveService.isGrabComplete(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-码垛完成:" + (plcDirectiveService.isPalletizingComplete(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-条形码读取触发:" + (plcDirectiveService.isScanSignal(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-条码:" + plcDirectiveService.getScanWheelNo(area));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-100抓取位有轮子:" + (plcDirectiveService.is100HaveWheel(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-101抓取位有轮子:" + (plcDirectiveService.is101HaveWheel(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-机器状态:" + (plcDirectiveService.getRobotStatus(area) ? "关闭" : "开启"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-100高低轮:" + WheelHeight.getByID(plcDirectiveService.getProductTypeToZeroGrab(area, 100)).getTypeName());
            list.add(WorkAreaType.getByID(area).getTypeName() + "-101高低轮:" + WheelHeight.getByID(plcDirectiveService.getProductTypeToZeroGrab(area, 101)).getTypeName());
            list.add(WorkAreaType.getByID(area).getTypeName() + "-产品类别:" + WheelHeight.getByID(plcDirectiveService.getProductType(area)).getTypeName());
            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES下发任务完成:" + (plcDirectiveService.getMesTask(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-任务位置:" + TaskLocation.getByID(plcDirectiveService.getTaskType(area)).getTypeName());
            list.add(WorkAreaType.getByID(area).getTypeName() + "-重复tag:" + packProductionLine.getRobot().getTagValue(area));
            list.add("缓存位高轮最大位置号:" + packProductionLine.getCacheArea().getMaxB80Num(area));
            list.add("缓存位低轮最大位置号:" + packProductionLine.getCacheArea().getMaxB60Num(area));
            list.add("缓存位下个高轮放位置号:" + packProductionLine.getCacheArea().nextPutLocationNo(2, area).get().getNo());
            list.add("缓存位下个低轮放位置号:" + packProductionLine.getCacheArea().nextPutLocationNo(1, area).get().getNo());
            list.add("=============");
            LambdaQueryWrapper<PkBoxLocation> pkBoxLocationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (area == 1) {
                pkBoxLocationLambdaQueryWrapper.between(PkBoxLocation::getId, packProductionLine.getProductArea().getLeftMinNo(), packProductionLine.getProductArea().getLeftMaxNo());
            } else {
                pkBoxLocationLambdaQueryWrapper.between(PkBoxLocation::getId, packProductionLine.getProductArea().getRightMinNo(), packProductionLine.getProductArea().getRightMaxNo());
            }
            List<PkBoxLocation> pkBoxLocations = pkBoxLocationMapper.selectList(pkBoxLocationLambdaQueryWrapper);
            for (PkBoxLocation pkBoxLocation : pkBoxLocations) {
                list.add(pkBoxLocation.getBoxLocationName() + "拍照完成:" + (plcDirectiveService.isPhotoFinish(pkBoxLocation.getPalletPhotoFinish()) ? "是" : "否"));
                list.add(pkBoxLocation.getBoxLocationName() + "托盘放置:" + (plcDirectiveService.isTrue(pkBoxLocation.getPalletComing()) ? "是" : "否"));
                list.add(pkBoxLocation.getBoxLocationName() + "托盘拿走:" + (plcDirectiveService.isTrue(pkBoxLocation.getPalletMove()) ? "是" : "否"));
                list.add(pkBoxLocation.getBoxLocationName() + "码垛数量:" + (plcDirectiveService.getPalletPackingCount(pkBoxLocation.getPalletPackingCount())));
                list.add(pkBoxLocation.getBoxLocationName() + "托盘类型:" + (WheelHeight.getByID(plcDirectiveService.getPalletWheelNumSet(pkBoxLocation.getPalletWheelNumSet()))).getTypeName());
                list.add("=============");
            }
        } else {
            list.add(WorkAreaType.getByID(area).getTypeName() + "-触发MES解析条码:" + (plcDirectiveService.isShuntAreaSignal(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析条码完成:" + (plcDirectiveService.isCodeParsingSucceeded(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析条码错误:" + (plcDirectiveService.isCodeParsingFailed(area) ? "是" : "否"));
            list.add("=============");
            list.add(WorkAreaType.getByID(area).getTypeName() + "-触发MES解析卡扣:" + (plcDirectiveService.isSnapSignal(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析卡扣完成:" + (plcDirectiveService.isSnapParsingSucceeded(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析卡扣错误:" + (plcDirectiveService.isSnapParsingFailed(area) ? "是" : "否"));
            list.add("=============");
            list.add(WorkAreaType.getByID(area).getTypeName() + "-条形码:" + plcDirectiveService.getShuntAreaScanWheelNo(area));
            list.add(WorkAreaType.getByID(area).getTypeName() + "分流完成:" + (plcDirectiveService.isShuntOver(area) ? "是" : "否"));
            list.add(WorkAreaType.getByID(area).getTypeName() + "-高低轮:" + WheelHeight.getByID(plcDirectiveService.getProductTypeToShunt(area)).getTypeName());
            list.add(WorkAreaType.getByID(area).getTypeName() + "-心跳:" + (plcDirectiveService.getPlcStatus() ? "异常" : "正常"));
        }
        return list;
    }
//    @Override
//    public List<String> status(int area) {
//        List<String> list = new ArrayList<>();
//        if (area < 3) {
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-可以下发任务:" + "可以");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-缓存位-装箱位是否工作:" + "工作");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-抓取完成:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-码垛完成:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-条形码读取触发:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-条码:" + "XDB111111");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-抓取位有轮子:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-机器状态:" + "关闭");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-高低轮:" + "高");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-产品类别:" + "高");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES下发任务完成:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-任务位置:" + "缓存位-装箱位");
//            list.add("缓存位高轮最大位置号:" + packProductionLine.getCacheArea().getMaxB80Num(area));
//            list.add("缓存位低轮最大位置号:" + packProductionLine.getCacheArea().getMaxB60Num(area));
//            list.add("缓存位下个高轮放位置号:" + packProductionLine.getCacheArea().nextPutLocationNo(2, area).get().getNo());
//            list.add("缓存位下个低轮放位置号:" + packProductionLine.getCacheArea().nextPutLocationNo(1, area).get().getNo());
//            list.add("=============");
//            LambdaQueryWrapper<PkBoxLocation> pkBoxLocationLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            if (area == 1) {
//                pkBoxLocationLambdaQueryWrapper.between(PkBoxLocation::getId, 1, 10);
//            } else {
//                pkBoxLocationLambdaQueryWrapper.between(PkBoxLocation::getId, 11, 19);
//            }
//            List<PkBoxLocation> pkBoxLocations = pkBoxLocationMapper.selectList(pkBoxLocationLambdaQueryWrapper);
//            for (PkBoxLocation pkBoxLocation : pkBoxLocations) {
//                list.add(pkBoxLocation.getBoxLocationName() + "拍照完成:" + "是");
//                list.add(pkBoxLocation.getBoxLocationName() + "托盘放置:" + "是");
//                list.add(pkBoxLocation.getBoxLocationName() + "托盘拿走:" + "是");
//                list.add("=============");
//            }
//        } else {
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-触发MES解析条码:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析条码完成:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析条码错误:" + "是");
//            list.add("=============");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-触发MES解析卡扣:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析卡扣完成:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-MES解析卡扣错误:" + "是");
//            list.add("=============");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-条形码:" + "ABCDEFG");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "分流完成:" + "是");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-高低轮:" + "高");
//            list.add(WorkAreaType.getByID(area).getTypeName() + "-心跳:" + "异常");
//        }
//        return list;
//    }
}
